package com.ghc.androidexample;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.ghc.androidexample.gcm.GCMActivity;
import com.ghc.androidexample.location.LocationActivity;
import com.ghc.androidexample.navigation.NavigationActivity;
import com.ghc.androidexample.share.ExFileSelectorActivity;
import com.ghc.androidexample.share.PhotoActivity;
import com.ghc.androidexample.user.ContactsActivity;

public class MainActivity extends Activity {
	private static final String INTERNAL_BUTTON_TEXT = "Create internal file (%d created)";
	private static final String EXTERNAL_BUTTON_TEXT = "Create external file (%d created)";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		File file = new File(getFilesDir(), "images");
		int count = countFile(file);
		String text = String.format(INTERNAL_BUTTON_TEXT, count);
		Button btn = (Button) findViewById(R.id.btnCount);
		btn.setText(text);
		
		file = new File(
				getExternalFilesDir(Environment.DIRECTORY_PICTURES), "images");
		count = countFile(file);
		text = String.format(EXTERNAL_BUTTON_TEXT, count);
		btn = (Button) findViewById(R.id.btnExCount);
		btn.setText(text);
		
		Intent intent = getIntent();
		String action = intent.getAction();
		String type = intent.getType();
		if("com.ghc.androidexample".equalsIgnoreCase(action)
				&& type != null && type.equals("text/plain")) {
			String data = intent.getStringExtra(Intent.EXTRA_TEXT);
			Log.d("ChungPV1", String.format("%s received: %s", MainActivity.class.getSimpleName(), data));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onShowNFC(View v) {
		Intent intent = new Intent(this, ExFileSelectorActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	public void onTakePhoto(View v) {
		Intent intent = new Intent(this, PhotoActivity.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	public void onCreateInternalFile(View v) {
		File file = new File(getFilesDir(), "images");
		createFile(file);
		int count = countFile(file);
		String text = String.format(INTERNAL_BUTTON_TEXT, count);
		Button btn = (Button) findViewById(R.id.btnCount);
		btn.setText(text);
	}

	public void onCreateExternalFile(View v) {
		File file = new File(
				getExternalFilesDir(Environment.DIRECTORY_PICTURES), "images");
		createFile(file);
		int count = countFile(file);
		String text = String.format(EXTERNAL_BUTTON_TEXT, count);
		Button btn = (Button) findViewById(R.id.btnExCount);
		btn.setText(text);
	}

	public void onGCM(View v) {
		Intent i = new Intent(this, GCMActivity.class);
		i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(i);
	}

	public void onContact(View v) {
		Intent intent = new Intent(this, ContactsActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	public void onLocation(View v) {
		Intent intent = new Intent(this, LocationActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	public void onNavigation(View v) {
		Intent intent = new Intent(this, NavigationActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(intent);
	}
	
	public void createFile(File file) {
		try {
			Random rd = new Random();
			String fileName = "";
			boolean exist = true;
			// Create directory
			if (!file.exists()) {
				file.mkdir();
			}
			do {
				fileName = String.format("defaultImage%d.jpg",
						rd.nextInt(1000000000));
				File test = new File(file, fileName);
				exist = test.exists();
			} while (exist);
			InputStream inputStream = getResources().getAssets().open(
					"defaultImage.jpg");
			FileOutputStream fileOutputStream = new FileOutputStream(new File(
					file, fileName));
			byte[] data = new byte[1024];
			while (inputStream.read(data, 0, data.length) != -1) {
				fileOutputStream.write(data);
			}
			inputStream.close();
			fileOutputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private int countFile(File file) {
		int count = 0;
		File[] files = file.listFiles();
		if (files != null) {
			count = files.length;
		}
		return count;
	}
}
